#include "Protocol.h"
// #include "CtrlBox.h"
#include <iostream>
#include <UDPSocket.h>
// CtrlBox ctrl_box;
UDPSocket udpSocket;

void reverseMemcpy(void* dst, const void* src, size_t size)
{
    char* dstChar = static_cast<char*>(dst);
    const char* srcChar = static_cast<const char*>(src);

    // 从源内存和目标内存的末尾开始，向开始方向拷贝
    for (size_t i = 0; i < size; ++i)
    {
        dstChar[size-1-i] = srcChar[i];
    }
}

void AnalysisData( BoxStatus *boxStatus, char *data )
{

    reverseMemcpy( &boxStatus->Head,            data,           sizeof(uint) );
    reverseMemcpy( &boxStatus->Cmd,             data + 4,       sizeof(uint) );
    reverseMemcpy( &boxStatus->DataLen,         data + 4 * 2,   sizeof(uint) );
    reverseMemcpy( &boxStatus->PeriodCnt,       data + 4 * 3,   sizeof(uint) );
    reverseMemcpy( &boxStatus->State,           data + 4 * 4,   sizeof(uint) );
    reverseMemcpy( &boxStatus->Reserve1,        data + 4 * 5,   sizeof(uint) );
    reverseMemcpy( &boxStatus->FaultWord_SMP,   data + 4 * 6,   sizeof(uint) );
    reverseMemcpy( &boxStatus->FaultWord_BIT,   data + 4 * 7,   sizeof(uint) );
    reverseMemcpy( &boxStatus->FaultWord_Reserve, data + 4 *8,  sizeof(uint) );
    reverseMemcpy( &boxStatus->Reserve2, data + 4 *9,           sizeof(uint) );

    reverseMemcpy( &boxStatus->Ratio_Lvdt0,  data + 4*10,       sizeof(float) );
    reverseMemcpy( &boxStatus->Lvdt0,  data + 4*11,       sizeof(float) );
    reverseMemcpy( &boxStatus->Lvdt0_Va,  data + 4*12,       sizeof(float) );
    reverseMemcpy( &boxStatus->Lvdt0_Vb,  data + 4*13,       sizeof(float) );
    reverseMemcpy( &boxStatus->Ratio_Lvdt1,  data + 4*14,       sizeof(float) );
    reverseMemcpy( &boxStatus->Lvdt1,  data + 4*15,       sizeof(float) );

    reverseMemcpy( &boxStatus->Lvdt1_Va,  data + 4*16,       sizeof(float) );
    reverseMemcpy( &boxStatus->Lvdt1_Vb,  data + 4*17,       sizeof(float) );
    reverseMemcpy( &boxStatus->FreqLvdt0Exci,  data + 4*18,       sizeof(float) );

    reverseMemcpy( &boxStatus->FreqLvdt1Exci,  data + 4*19,       sizeof(float) );
    reverseMemcpy( &boxStatus->VoltageLvdt0Exci,  data + 4*20,       sizeof(float) );
    reverseMemcpy( &boxStatus->VoltageLvdt1Exci,  data + 4*21,       sizeof(float) );
    reverseMemcpy( &boxStatus->Voltage27V,  data + 4*22,       sizeof(float) );

    reverseMemcpy( &boxStatus->Voltage15V_A,  data + 4*23,       sizeof(float) );
    reverseMemcpy( &boxStatus->Voltage15V_B,  data + 4*24,       sizeof(float) );


    reverseMemcpy( &boxStatus->Voltage3d3V,  data + 4*25,       sizeof(float) );
    reverseMemcpy( &boxStatus->VoltageConstant1,  data + 4*26,       sizeof(float) );
    reverseMemcpy( &boxStatus->VoltageConstant2,  data + 4*27,       sizeof(float) );
    reverseMemcpy( &boxStatus->VoltageConstant3,  data + 4*28,       sizeof(float) );
    reverseMemcpy( &boxStatus->Reserve9,  data + 4*29,       sizeof(float) );
    reverseMemcpy( &boxStatus->SoftVersion,  data + 4*30,       sizeof(uint) );
    reverseMemcpy( &boxStatus->CheckSum,  data + 4*31,       sizeof(uint) );



}

bool openHost(const char *ipAddr, int port)
{
    // ctrl_box.setIPAddress( ipAddr, port );

    udpSocket.setServerAddress( ipAddr, port );

    return true;
}

void closeHost()
{

}

void setLocalPort(int port)
{
    udpSocket.setLocalPort( port );
}

BoxStatus readSingleStatus(bool *ok)
{
    char cmd[3] = { 0x03, 0x16, 0x19 };

    QByteArray byte(cmd,3);

    udpSocket.sendData( byte );

    return readBoxStatus( ok );
}

bool setContinuousCMD()
{
    char cmd[3] = { 0x03, 0x17, 0x1a };

    QByteArray byte(cmd,3);

    return udpSocket.sendData( byte );
}

bool stopContinuousCMD()
{
    char cmd[3] = { 0x03, 0x37, 0x3a };

    QByteArray byte(cmd,3);

    return udpSocket.sendData( byte);
}

BoxStatus readBoxStatus( bool *ok )
{
    BoxStatus boxStatus;

    auto data = udpSocket.readData(  );


    AnalysisData( &boxStatus, data.data() );

    if( ok )
    {
        *ok = true;
    }

    return boxStatus;


    if( ok )
    {
        *ok = false;
    }

    return boxStatus;
}

bool readLvdt0CalibratData(float *data, unsigned int size)
{
    char cmd[4] = { 0x04, 0x19, 0x00, 0x00 };

    cmd[3] = size;

    QByteArray byte(cmd,4);

    bool res = udpSocket.sendData( byte );

    if( res && data != nullptr)
    {
        // ctrl_box.readData(  )
    }
    return false;
}

bool readLvdt1CalibratData(float *data, unsigned int size)
{
    char cmd[4] = { 0x04, 0x19, 0x01, 0x00 };

    cmd[3] = size;

    QByteArray byte(cmd,4);

    bool res = udpSocket.sendData( byte );

    if( res && data != nullptr)
    {
        // ctrl_box.readData(  )
    }
    return false;
}

int testAdd(int a, int b)
{
    std::cout<<"testAdd a : "<< a << " b : "<< std::endl ;
    return a+b;
}

int testArray( float *data, unsigned int size)
{
    for( int i = 0; i < size; i++ )
    {
        data[i] = i;
    }
    return 111;
}


